package com.code.leetcode.before50;

import java.util.HashMap;
import java.util.Map;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/19 15:53
 */
public class The13 {

    Map<Character, Integer> map;
    {
        map = new HashMap<Character, Integer>() {
            {
                put('I', 1);
                put('V', 5);
                put('X', 10);
                put('L', 50);
                put('C', 100);
                put('D', 500);
                put('M', 1000);
            }
        };
    }

    public int romanToInt(String s) {
        int rtn = 0;
        if (s == null || s.length() < 1) return rtn;
        int len = s.length();
        int num = map.get(s.charAt(0));
        for (int i = 1; i < len; i++) {
            int n = map.get(s.charAt(i));
            if (n > num) rtn -= num;
            else rtn += num;
            num = n;
        }
        return rtn + num;
    }

}
